<?php
require_once( dirname(__FILE__) . '/wp-load.php' );
wp();

// var_dump($wp);
// echo $wp->request;

if ($_POST) {
	$post_id = 14;
	$msg = array(
		'type' => '菜鸟计划',
		'name' => '',
		'email' => '',
		'phone' => '',
		'company' => '',
		'msg' => '',
	);
	$msg = wp_parse_args($_POST, $msg);
// 	var_dump($msg);die();


	$comment_content = "";
	foreach ($msg as $key=>$pd) {
		if ($comment_content != "") {
			$comment_content .= "<br/>";
		}
		$comment_content .= "$key = $pd";
	}
	$data = array(
		'comment_post_ID' => $post_id,
		'comment_author' => $msg['name'],
		'comment_author_email' => $msg['email'],
		'comment_author_url' => '',
		'comment_author_IP' => preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] ),
		'comment_date' => current_time('mysql'),
		'comment_date_gmt' => current_time('mysql', 1),//get_gmt_from_date($comment_date),
		'comment_content' => $comment_content,
		'comment_karma' => 0,
		'comment_approved' => 1,
		'comment_agent' => substr($_SERVER['HTTP_USER_AGENT'], 0, 254),
		'comment_type' => '',
		'comment_parent' => 0,
		'user_id' => 0,
	);
	$wpdb->insert($wpdb->comments, $data);
	$id = (int) $wpdb->insert_id;

	$admin = get_user_by('login', 'admin');
	mail($admin->user_email, 'New Message from Moretong Corp-Plan', $comment_content);

	setcookie('msg_saved', '1');
	header('Location: /personal-plan.html');
}
